// pages/paryment/index.js
const app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        primary: app.globalData.primary, // 主题色
        ossPath: app.globalData.ossPath,
        state: 0,
        orderCode: null, // 订单号
        amount: 0,
        info: null
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        //设置主题色
        if (app.globalData.primary) {
            this.setData({ primary: app.globalData.primary })
            app.usePrimary(app.globalData.primary)
        }
        this.setData({
            orderCode: options.orderCode
        })
        this.getOrder()
        this.getPaymentState()
    },
    getOrder: function() { // 获取订单
        wx.request({
            url: app.globalData.serverPath + '/Order?orderCode=' + this.data.orderCode,
            success: ({
                data
            }) => {
                this.setData({
                    info: data,
                    amount: parseFloat(data.Amount).toFixed(2)
                })
                this.payment()
            }
        })
    },
    getPaymentState() { // 获取支付状态
        wx.showLoading({title: '查询订单'})
        let timer = setInterval(() => {
            wx.request({
                url: app.globalData.serverPath + '/CheckPay?OrderCode=' + this.data.orderCode + '&Action=PrintShop',
                success: ({
                    data
                }) => {
                    clearInterval(timer)
                    if (data == 'Success') {
                        this.setData({
                            state: 2
                        })
                    }
                    if (data == 'Fail') {
                        this.setData({
                            state: 1
                        })
                        // this.payment()
                    }
                },
                fail: err => {
                    this.setData({
                        state: -1
                    })
                },
                complete: () => wx.hideLoading()
            })
        }, 1000)

    },
    payment: function() {
        wx.request({
            url: app.globalData.serverPath + '/payment/wxmini',
            method: 'post',
            header: app.globalData.header,
            data: 'OrderCode=' + this.data.orderCode,
            success: ({
                data
            }) => {
                console.log(data)
                if (data.status == 'success') {
                    wx.requestPayment({
                        timeStamp: data.result.timeStamp,
                        nonceStr: data.result.nonceStr,
                        package: data.result.package,
                        signType: data.result.signType,
                        paySign: data.result.paySign,
                        success: ({data}) => {
                            this.setData({
                                state: 2
                            })
                        },
                        fail:({data}) => {
                            console.log(data)
                            this.getPaymentState();
                        }
                    })
                }
                if (data.status === 'fail') {
                    wx.showModal({
                        title: '提示',
                        content: data.msg
                    })
                }
            }
        })
        // wx.requestPayment({
        //     timeStamp: '',
        //     nonceStr: '',
        //     package: '',
        //     signType: 'MD5',
        //     paySign: '',
        //     success(res) { },
        //     fail(res) { }
        // })
    },
    toview: function() { // 查看订单
        wx.navigateTo({
            url: '../orderdetail/orderdetail?n=' + this.data.info.OrderId
        })
    },
    toHome: function() { // 返回首页
        wx.reLaunch({
            url: '../index/index',
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {},

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})